package com.project.controller;

import com.project.entity.KnowLedge;
import com.project.entity.Result;
import com.project.service.KnowLedgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/knowledge")
public class KnowLedgeController {
    @Autowired
    private KnowLedgeService knowLedgeService;

    @GetMapping("/page/{pageIndex}/{pageItemCount}")
    public Result findPage(@PathVariable Integer pageIndex,@PathVariable Integer pageItemCount) {
        if(pageItemCount == null){
            pageItemCount = 4;
        }
        Result result = new Result();
        result.success("查询成功");


        List<KnowLedge> knowLedgeList = knowLedgeService.findPage(pageIndex, pageItemCount);


        result.putData("knowLedgeList",knowLedgeList);
        return result;
    }

    @GetMapping("findKnowLedgeById/{id}")
    public Result findOneById(@PathVariable("id") Integer id){
        Result result = new Result();
        try {
            KnowLedge oneById = knowLedgeService.findOneById(id);
            result.success("查询成功");
            result.putData("knowLedge",oneById);
        }catch (Exception e){
            e.printStackTrace();
            result.error("错误");
        }
        return result;
    }
}
